package com.lagou.demo;

import com.lagou.mapper.OrdersMapper;
import com.lagou.mapper.UserMapper;
import com.lagou.pojo.Order;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import tk.mybatis.mapper.entity.Example;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class MyBatisMapperTest {

    private OrdersMapper ordersMapper;
    private SqlSession sqlSession;

    @Before
    public void before() throws IOException {
        InputStream inputStream = Resources.getResourceAsStream("sqlMapConfig.xml");
        SqlSessionFactory build = new SqlSessionFactoryBuilder().build(inputStream);
        sqlSession = build.openSession();
        ordersMapper = sqlSession.getMapper(OrdersMapper.class);
    }

    @After
    public void after() {
        sqlSession.close();
    }

    @Test
    public void mapper(){
        List<Order> orders = ordersMapper.findAll();
        orders.forEach(order1 -> System.out.println(order1));
        Order order = new Order();
        order.setUid(1);
        order.setOrdertime("2022-12-01");
        order.setTotal("20.00");
        ordersMapper.insert(order);
        sqlSession.commit();
        orders = ordersMapper.findAll();
        orders.forEach(order1 -> System.out.println(order1));
    }

    @Test
    public void example(){
        Example example = new Example(Order.class);
        example.createCriteria().andEqualTo("id", 1);
        List<Order> orders = ordersMapper.selectByExample(example);
        orders.forEach(order1 -> System.out.println(order1));
    }
}
